/**
 * 首页
 * @author Qasim <15750783791@163.com>
 * @version 2016/10/29 
 */

document.addEventListener('plusready' , ready , false);

var self = memberInfo = index_info = ''; 
function ready(){
	self = plus.webview.currentWebview(); 
	
	plus.storage.setItem('index_page' , self.id);
	  
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);  
	$('.avatar').attr('src' , getImgUrl(memberInfo.avatar))
	if(memberInfo.nickname) $('.nickname').text(memberInfo.nickname)
	if(memberInfo.signature) $('.signature').text(memberInfo.signature)
	
	//前往修改个人信息 
	$('.bbs-pulish-img img').on('tap',function(){
		var parameter = {page : self.id};  
		goUrl('person-info.html' , 'person_info' , parameter );
	})
	
	//获取缓存的数据
	index_info = plus.storage.getItem('index_info');
	if(index_info){//如果有 直接显示
		showWaiting('加载中...');
		showInfo();
	}else{//没有 向服务器发送请求
		info(true);
	}
	
	//我的书架
	myBookself(memberInfo.aid , 1);  
	
	//前往各类书籍
	$('.classifies').on('tap' , function(){
		if($('.classifies').index($(this)) == 0){
			goUrl('tuijian.html' , 'tuijian');
		}else{
			var parameter = {tid : 36};
			goUrl('new-books.html' , 'new_books' , parameter);
		}
	})
	
	//前往搜索
	$(".search").on('tap',function(){
		goUrl("search.html" , "search");
	})
	
}


/**
 * 加载首页数据
 */
function info(reload){  
	$.ajax({
		url : apiRoot + '/Home/Article/articleInfo/' , 
		type : 'post' , 
		beforeSend:function(){
			plus.nativeUI.showWaiting('加载中...');
		},
		complete:function(){  
			plus.nativeUI.closeWaiting();  
		},
		success : function(data){ 
			plus.storage.setItem('index_info' , data + '');
			if(reload){
				showInfo();
			}
		} , 
		error : function(e){
			errorPrompt(e);
		}
	})
	
}


function showInfo(){  
	setTimeout(function(){
		info(false);
	} , 1000);
	result = $.parseJSON(index_info);
	//轮播图
	if(result.carousel != ''){ 
		$('.carousel img').each(function(index){ 
			switch (index){ 
				case 0:
					$('.mui-slider-item-duplicate img').eq(0).attr('src' , getImgUrl(result.carousel[index].adfile, '../public/img/11.jpg'));
					$(this).attr('src' ,  getImgUrl(result.carousel[index].adfile) , '../public/img/11.jpg')
					break;
				case 1:
					$(this).attr('src' ,  getImgUrl(result.carousel[index].adfile , '../public/img/11.jpg')) 
					break;
				case 2:
					$('.mui-slider-item-duplicate img').eq(1).attr('src' , getImgUrl(result.carousel[index].adfile, '../public/img/11.jpg'));
					$(this).attr('src' ,  getImgUrl(result.carousel[index].adfile) , '../public/img/11.jpg')
					break;
			}
		})
	}
	 
	//推荐书籍 
	if(result.recommend != ''){
		$('.mui-col-xs-6').each(function(index){
			switch (index){ 
				case 0: 
					$(this).attr('data-id' , result.recommend[index].id).attr('data-price' ,result.recommend[index].price);
					$(this).find('img').attr('src' , getImgUrl(result.recommend[index].litpic, '../public/img/book.jpg'));
					if(result.recommend[index].book_name)$(this).find('.book_name').text(result.recommend[index].book_name);
					if(result.recommend[index].description)$(this).find('p').text(result.recommend[index].description);
					break;
				case 1:
					$(this).attr('data-id' , result.recommend[index].id).attr('data-price' ,result.recommend[index].price);
					$(this).find('img').attr('src' ,  getImgUrl(result.recommend[index].litpic , '../public/img/book.jpg'))
					if(result.recommend[index].book_name)$(this).find('.book_name').text(result.recommend[index].book_name);
					if(result.recommend[index].description)$(this).find('p').text(result.recommend[index].description);
					break;
			}
			$(this).on('tap' , function(){
				bookDetails($(this).attr('data-id') ,$(this).attr('data-price') );
			})
		})
	}
	
	//新书
	if(result.new != ''){
		$('.new-book-list').each(function(index){
			switch (index){ 
				case 0:
					$(this).attr('data-id' , result.new[index].id).attr('data-price' ,result.new[index].price).addClass('book'+result.new[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.new[index].litpic, '../public/img/book.jpg'));
					if(result.new[index].book_name)$(this).find('.book_name').text(result.new[index].book_name);
					if(result.new[index].description)$(this).find('.description').text(result.new[index].description);
					if(result.new[index].author)$(this).find('.author').text(result.new[index].author);
					if(result.new[index].hits)$(this).find('.hits').text(result.new[index].hits);
					break; 
				case 1:
					$(this).attr('data-id' , result.new[index].id).attr('data-price' ,result.new[index].price).addClass('book'+result.new[index].id);
					$(this).find('img').attr('src' ,  getImgUrl(result.new[index].litpic , '../public/img/book.jpg'))
					if(result.new[index].book_name)$(this).find('.book_name').text(result.new[index].book_name);
					if(result.new[index].description)$(this).find('.description').text(result.new[index].description);
					if(result.new[index].author)$(this).find('.author').text(result.new[index].author);
					if(result.new[index].hits)$(this).find('.hits').text(result.new[index].hits);
					break;
			}
			$(this).on('tap' , function(){
				bookDetails($(this).attr('data-id') ,$(this).attr('data-price') );
			})
		})
	}
	
	//专题名称 
	if(result.special != ''){ 
		$('.special').each(function(index){   
			switch (index){   
				case 0:  
					$(this).text(result.special[index].classname);
					break;
				case 1:  
					$(this).text(result.special[index].classname);
					break;
				case 2:
					$(this).text(result.special[index].classname);
					break;
			}
		})
	}
	
	
	//专题一
	if(result.special_1 != ''){
		$('.special_1').each(function(index){
			switch (index){ 
				case 0:
					$(this).attr('data-id' , result.special_1[index].id).attr('data-price' ,result.special_1[index].price).addClass('book'+result.special_1[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_1[index].litpic, '../public/img/book.jpg'));
					if(result.special_1[index].book_name)$(this).find('.book_name').text(result.special_1[index].book_name);
					if(result.special_1[index].description)$(this).find('.description').text(result.special_1[index].description);
					if(result.special_1[index].author)$(this).find('.author').text(result.special_1[index].author);
					if(result.special_1[index].hits)$(this).find('.hits').text(result.special_1[index].hits);
					break;
				case 1:
					$(this).attr('data-id' , result.special_1[index].id).attr('data-price' ,result.special_1[index].price).addClass('book'+result.special_1[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_1[index].litpic, '../public/img/book.jpg'));
					if(result.special_1[index].book_name)$(this).find('.book_name').text(result.special_1[index].book_name);
					if(result.special_1[index].description)$(this).find('.description').text(result.special_1[index].description);
					if(result.special_1[index].author)$(this).find('.author').text(result.special_1[index].author);
					if(result.special_1[index].hits)$(this).find('.hits').text(result.special_1[index].hits);
					break;
				case 2:
					$(this).attr('data-id' , result.special_1[index].id).attr('data-price' ,result.special_1[index].price).addClass('book'+result.special_1[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_1[index].litpic, '../public/img/book.jpg'));
					if(result.special_1[index].book_name)$(this).find('.book_name').text(result.special_1[index].book_name);
					if(result.special_1[index].description)$(this).find('.description').text(result.special_1[index].description);
					if(result.special_1[index].author)$(this).find('.author').text(result.special_1[index].author);
					if(result.special_1[index].hits)$(this).find('.hits').text(result.special_1[index].hits);
					break;
			}
			$(this).on('tap' , function(){
				bookDetails($(this).attr('data-id') ,$(this).attr('data-price') );
			})
		})
	}
	//专题二
	if(result.special_2 != ''){
		$('.special_2').each(function(index){
			switch (index){   
				case 0:
					$(this).attr('data-id' , result.special_2[index].id).attr('data-price' ,result.special_2[index].price).addClass('book'+result.special_2[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_2[index].litpic, '../public/img/book.jpg'));
					if(result.special_2[index].book_name)$(this).find('.book_name').text(result.special_2[index].book_name);
					if(result.special_2[index].description)$(this).find('.description').text(result.special_2[index].description);
					if(result.special_2[index].author)$(this).find('.author').text(result.special_2[index].author);
					if(result.special_2[index].hits)$(this).find('.hits').text(result.special_2[index].hits);
					break;
				case 1:
					$(this).attr('data-id' , result.special_2[index].id).attr('data-price' ,result.special_2[index].price).addClass('book'+result.special_2[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_2[index].litpic, '../public/img/book.jpg'));
					if(result.special_2[index].book_name)$(this).find('.book_name').text(result.special_2[index].book_name);
					if(result.special_2[index].description)$(this).find('.description').text(result.special_2[index].description);
					if(result.special_2[index].author)$(this).find('.author').text(result.special_2[index].author);
					if(result.special_2[index].hits)$(this).find('.hits').text(result.special_2[index].hits);
					break;
				case 2:
					$(this).attr('data-id' , result.special_2[index].id).attr('data-price' ,result.special_2[index].price).addClass('book'+result.special_2[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_2[index].litpic, '../public/img/book.jpg'));
					if(result.special_2[index].book_name)$(this).find('.book_name').text(result.special_2[index].book_name);
					if(result.special_2[index].description)$(this).find('.description').text(result.special_2[index].description);
					if(result.special_2[index].author)$(this).find('.author').text(result.special_2[index].author);
					if(result.special_2[index].hits)$(this).find('.hits').text(result.special_2[index].hits);
					break;
			}
			$(this).on('tap' , function(){
				bookDetails($(this).attr('data-id') ,$(this).attr('data-price') );
			})
		})
	} 
	//专题三
	if(result.special_3 != ''){
		$('.special_3').each(function(index){
			switch (index){ 
				case 0:
					$(this).attr('data-id' , result.special_3[index].id).attr('data-price' ,result.special_3[index].price).addClass('book'+result.special_3[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_3[index].litpic, '../public/img/book.jpg'));
					if(result.special_3[index].book_name)$(this).find('.book_name').text(result.special_3[index].book_name);
					if(result.special_3[index].description)$(this).find('.description').text(result.special_3[index].description);
					if(result.special_3[index].author)$(this).find('.author').text(result.special_3[index].author);
					if(result.special_3[index].hits)$(this).find('.hits').text(result.special_3[index].hits);
					break;
				case 1:
					$(this).attr('data-id' , result.special_3[index].id).attr('data-price' ,result.special_3[index].price).addClass('book'+result.special_3[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_3[index].litpic, '../public/img/book.jpg'));
					if(result.special_3[index].book_name)$(this).find('.book_name').text(result.special_3[index].book_name);
					if(result.special_3[index].description)$(this).find('.description').text(result.special_3[index].description);
					if(result.special_3[index].author)$(this).find('.author').text(result.special_3[index].author);
					if(result.special_3[index].hits)$(this).find('.hits').text(result.special_3[index].hits);
					break; 
				case 2:
					$(this).attr('data-id' , result.special_3[index].id).attr('data-price' ,result.special_3[index].price).addClass('book'+result.special_3[index].id);
					$(this).find('img').attr('src' , getImgUrl(result.special_3[index].litpic, '../public/img/book.jpg'));
					if(result.special_3[index].book_name)$(this).find('.book_name').text(result.special_3[index].book_name);
					if(result.special_3[index].description)$(this).find('.description').text(result.special_3[index].description);
					if(result.special_3[index].author)$(this).find('.author').text(result.special_3[index].author);
					if(result.special_3[index].hits)$(this).find('.hits').text(result.special_3[index].hits);
					break;
			} 
			$(this).on('tap' , function(){
				bookDetails($(this).attr('data-id') ,$(this).attr('data-price') );
			})
		})
	} 
}

 
/**
 * 监听子页面透传数据
 */ 

window.addEventListener('update_personal_info' , function(result){
	$('.avatar').attr('src' , getImgUrl(result.detail.avatar))
	if(result.detail.nickname) $('.nickname').text(result.detail.nickname)
	if(result.detail.signature) $('.signature').text(result.detail.signature)
})
/**
 * 更新点击量
 */ 
window.addEventListener('update_hits' , function(result){  
	$('.book'+result.detail.aid+' .hits').text(parseInt($('.book'+result.detail.aid + ' .hits').text()) + 1);
})